# -*- coding: utf-8 -*-#
#-------------------------------------------------------------------------------
# 建立者:        潘炎珍  
# Name:         hw01
# Description:
# Author:       98745
# Date:         2019/4/14
#-------------------------------------------------------------------------------

import jieba
import re
from jieba import analyse

class File:
    def open_file(self):#打开需要分析的文件
        f = open('./inter_root.txt', 'a+', encoding='utf-8')
        return f

    def get_text(self,f):#取出文件的内容
        f.seek(0)
        text = f.read()
        return text

    def close_file(self, f):#关闭文件
        f.close()

    def write_file(self,data):#将分析的结果写入指定的文件
        f=open('./inter_result.txt','a+',encoding='utf-8')
        f.write(data+'\n')
        f.close()

    def analyse(self,text):#分析文件的内容
        # print(text)
        # text='李小福是创新办主任也是云计算方面的专家'#测试
        result=analyse.extract_tags(text,withWeight=True)
        for word,weight in result:
            # print('%s = %s' % (x,w))
            num=len(re.findall(word,text))
            # print('%s = %d' % (word,num))
            self.write_file('%s = %d' % (word,num))


if __name__ == "__main__":
    try:
        file=File()
        f=file.open_file()
        text=file.get_text(f)
        file.analyse(text)
        file.close_file(f)
    except Exception as e:
        print(e)


